package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

@SuppressWarnings("serial")
@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order>{
	//属性驱动获取省市区的信息
	private String recAreaInfo;
	private String sendAreaInfo;
	
	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	@Action(value="saveOrder",results={@Result(name="success",type="redirect",location="myhome.html")})
	public String saveOrder(){
		//寄件人的area信息
		String sendStr[] = sendAreaInfo.split("/");
		String recStr[] = recAreaInfo.split("/");
		Area sendArea = new Area();
		sendArea.setCity(sendStr[1]);
		sendArea.setDistrict(sendStr[2]);
		sendArea.setProvince(sendStr[0]);
		
		//收件人area信息
		Area recArea = new Area();
		recArea.setCity(recStr[1]);
		recArea.setDistrict(recStr[2]);
		recArea.setProvince(recStr[0]);
		
		//给order赋值
		model.setRecArea(recArea);
		model.setSendArea(sendArea);
		//获取用户sessionID
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());
		
		//调用bos_management保存订单信息
		WebClient.create(Constants.BOS_MANAGEMENT+"/services/orderService/orderSave")
					.type(MediaType.APPLICATION_JSON).post(model);
		return SUCCESS;
	}

}
